/**
 * Universita' degli Studi di Palermo, 2010-11, License GNU GPL 3 (http://www.gnu.org/licenses/gpl.html)
 */
package org.unipa.botplatform.aop.log;

import java.util.Arrays;
import java.util.Date;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * LogAspect manages every aspect concerning log in the application. 
 * @author Giuseppe Miceli
 *
 */
@Aspect
public class LogAspect {

	/** The logger for the program. */
	private static Logger logger = LogManager.getLogger(LogAspect.class);

	/**
	 * 
	 * @param jp
	 * @throws Throwable
	 */
	@AfterThrowing("execution(* *.*(..))")
	public void afterThrowing(JoinPoint jp) throws Throwable {
		logger.error("[" + new Date() + "]\t ERROR: " + jp.getSignature().getName());
	}

	/**
	 * 
	 * @param jp
	 */
	@Before("execution(public * org.unipa.cc..*(..))")
	public void beforeAdvice(JoinPoint jp) {
		logger.debug("[" + new Date() + "]\t Method Call, Entering " + jp.getTarget().getClass() + "."  +  jp.getSignature().getName()
			+  " with argument(s): " + Arrays.toString(jp.getArgs()));
	}

	/**
	 * 
	 * @param jp
	 * @param returnValue
	 * @throws Throwable
	 */
	@AfterReturning(
			pointcut = "execution(public * org.unipa.cc..*(..))",
			returning = "returnValue")
			public void afterReturning(JoinPoint jp, Object returnValue) throws Throwable {
		logger.debug("[" + new Date() + "]\t Method Call, Exiting " + jp.getTarget().getClass() + "."  +  jp.getSignature().getName()
			+  " with return value(s): " + returnValue);	
	}

	/**
	 * 
	 */
	public LogAspect() {
		super();
		PropertyConfigurator.configure("log4j.properties");
	}
}
